import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import CppModule

ApplicationWindow {
    width: 640
    height: 480
    visible: true
    // title: qsTr("QML 示例")
    title: qsTr("QML Interact C++")

    readonly property string constString : "string inside QML"
    property string labelText : constString

    ColumnLayout {
        Text {
            id: toggleText
            text: toggle ? qsTr("Hello, world!") : qsTr("Goodbye, world!")
            focus: true
            property bool toggle: true
            Keys.onReturnPressed: (event) => {
                toggleText.toggle = !toggleText.toggle
                event.accepted = true
            }
        }

        RowLayout {
            Label {
                text: labelText

                MouseArea {
                    anchors.fill: parent
                    hoverEnabled: true
                    onExited: {
                        labelText = constString
                    }
                }
            }

            Button {
                text: "点击接收C++的消息"
                onClicked: {
                    myObject.sendMessage()
                    labelText = myObject.message()
                }
            }
        }

        Text {
            id: currentTime
            text: "当前时间：" + new Date().toLocaleTimeString()
            font.pixelSize: 16
        }

        Timer {
            id: timer
            interval: 1000
            running: true
            repeat: true
            onTriggered: {
                currentTime.text = "当前时间：" + new Date().toLocaleTimeString()
            }
        }

        MyClass {
            id: myClass
        }

        Button {
            id: btnInteract
            text: qsTr("请调用C++方法！")
            onClicked: {
                btnInteract.text = myClass.sendMessage()
            }
        }

        Rectangle {
            width: 200
            height: 200
            color: "red"

            Rectangle {
                x: 100
                y: 100
                width: 100
                height: 100
                color: "blue"

                Rectangle {
                    width: 50
                    height: 50
                    color: "green"
                }
            }
        }

        RowLayout {
            spacing: 10
            // anchors.centerIn: parent

            Button {
                text: "点击我"
                onClicked: {
                    label.text = "你点击了按钮！"
                }
            }

            Label {
                id: label
                text: "这里将显示按钮点击的结果"
            }
        }

        Text {
            id: hello
            text: qsTr("你好，QML！")
            // anchors.centerIn: parent
            font.bold: true
            font { pointSize: 14; capitalization: Font.AllUppercase }

            Behavior on rotation {
                NumberAnimation { duration: 500 }
            }

            Rectangle {
                id: colorRect
                width: 20 * 2
                height: width
                radius: 20
                border.color: "green"

                anchors.left: hello.right
                anchors.leftMargin: 10
                anchors.verticalCenter: hello.verticalCenter

                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        hello.rotation += 360   // 旋转事件（点击绿色圆环，旋转360）
                        hello.color = parent.border.color
                    }

                    // 实现圆形交互特效
                    hoverEnabled: true
                    onEntered: {
                        parent.width = 32
                        parent.color = "black"
                    }
                    onExited: {
                        parent.width = 40
                        parent.color = "white"
                    }
                }
            }
        }
    }
}
